﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICStock : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        public string key = "";
        public string FBrNo = "";
        private string ID;


        public ICStock()
        {
            InitializeComponent();
        }
        public ICStock(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }

        private void ICStock_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();         
        }
      
        //获取数据
        private void BinData() 
        {
            DataSet ds = bjs.GetICStockBill();
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }      
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICStock";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "入库单号";
            cl2.MappingName = "FBrNo";
            cl2.Width = 110;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "物料名称";
            cl3.MappingName = "Fname";
            cl3.Width = 110;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "辅助属性";
            cl4.MappingName = "Fattr";
            cl4.Width = 90;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "规格型号";
            cl5.MappingName = "FModel";
            cl5.Width = 70;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "开单时间";
            cl6.MappingName = "Fdate";
            cl6.Width = 80;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
            cl7.HeaderText = "开单人";
            cl7.MappingName = "FBillerID";
            cl7.Width = 50;
            dts.GridColumnStyles.Add(cl7);

            dataGrid1.TableStyles.Add(dts);
 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData(); 
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Main main = new Main(this.ID);
            main.Show();
            this.Hide();
        }

        private void dataGrid1_DoubleClick(object sender, EventArgs e) 
        {
            //DataGrid dataGrid1 = sender as DataGrid;
            //int index = ((DataGrid)sender).CurrentCell.RowNumber;
            //if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            //{
            //    ((DataGrid)sender).Select(index);
            //}
            //key = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();

            //ICStockEntry icse = new ICStockEntry(key);
            //icse.Show();
            //this.Hide();
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FBrNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择要完成的订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
            }
            else 
            {
                //查询还有没有没扫描的信息
                DataSet set = bjs.GetICStockBillEntry(FBrNo);
                if (set.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("有未扫描信息，不能完成订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    DialogResult dr = MessageBox.Show("是否完成订单： " + FBrNo + " ？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    if (dr == DialogResult.Yes)
                    {
                        //同步到金蝶的方法
                        //int num = bjs.INSERT_ICStockBill(FBrNo);
                        //if (num > 0)
                        //{
                        string txt = "";
                        bool lb = bjs.FinishICStock(FBrNo, Program.user, Program.userid, out txt);
                        if (lb)
                        {
                            DataGridStyle();
                            BinData();
                        }
                        else
                        {

                            MessageBox.Show("完成订单错误！ " + txt, "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        }
                        //}
                        //else
                        //{
                        //    MessageBox.Show("完成订单错误,同步到金蝶时存在错误，请联系开发人查询处理", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        //}
                    }
                    else
                    {
                        return;
                    }
                }

            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            int num = 0;
            ICStockEntry icse = new ICStockEntry(FBrNo,this.ID,out num);
            if (num == 0) 
            {
                MessageBox.Show("该入库单的条码已经全部提交，请选择完成订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.Show();
                return;
            }
            else
            {
                icse.Show();
                this.Hide();
            }          
        }
    }
}